﻿using System;
using System.Buffers;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WCS_Service.DomianService.Interface;

namespace WCS_Service.DomianService.ComDomainService
{
    /// <summary>
    /// 根据指定字符解析
    /// </summary>
    public class FixedCharPackageFilter : IPackageFilter
    {
        private readonly char _splitChar;
        public FixedCharPackageFilter(char splitChar)
        {
            _splitChar = splitChar;
        }

        public ReadOnlySequence<byte> ResolvePackage(ref ReadOnlySequence<byte> sequence)
        {
            var position = sequence.PositionOf((byte)_splitChar);

            if (position == null)
            {
                return default;
            }
            else
            {
                var index = sequence.GetPosition(1, position.Value);
                var data = sequence.Slice(0, index);
                sequence = sequence.Slice(index);

                return data;
            }
        }
    }
}
